import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName Request
 * @Deseription TODO
 * @Author fx
 * @Date 2021/1/10 0010 16:08
 * @Version 1.0
 **/
public class Request {

    //请求方式 GET / POST
    private String method;

    //请求地址  /  /index.html
    private String url;

    // 输入流
    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request() {
    }

    // 传入输入流
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;


        int count = 0 ;
        // 从输入流中获取请求信息，如果获取的数据为空一直循环等待到获取到数据
        while(count == 0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        //获取第一行请求信息,根据回车符进行分割
        String firstLineStr = inputStr.split("\\n")[0];  // GET / HTTP/1.1
        String[] strings = firstLineStr.split(" ");  // 根据空格进行分割

        this.method = strings[0];
        this.url = strings[1];

        System.out.println("methos =========:"+method);
        System.out.println("url=============:"+url);
    }
}
